package com.hongyun.tms.common.validator;

import cn.hutool.core.util.IdcardUtil;
import com.hongyun.tms.common.annotation.IsIdCardNumber;
import org.apache.commons.lang3.StringUtils;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

/**
 * 身份证校验
 *
 * @author liPeng
 * @date 2024-06-06
 */
public class IdCardNumberValidator implements ConstraintValidator<IsIdCardNumber, String> {
    @Override
    public void initialize(IsIdCardNumber constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        if (StringUtils.isBlank(value)) {
            return true;
        }
        return IdcardUtil.isValidCard(value);
    }
}
